home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / SortedCollection.st < prev    next >
Text File  |  1991-09-12  |  4KB  |  152 lines

  1. "======================================================================
  2. |
  3. |   SortedCollection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Changed to use real method categories.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. OrderedCollection variableSubclass: #SortedCollection
  40.           instanceVariableNames: 'sortBlock'
  41.           classVariableNames: ''
  42.           poolDictionaries: ''
  43.           category: nil.
  44.  
  45. SortedCollection comment:
  46. 'I am a collection of objects, stored and accessed according to some
  47. sorting criteria.  I store things using a bubble sort.  My instances have a
  48. comparison block associated with them; this block takes two arguments and
  49. is a predicate which returns true if the first argument should be sorted
  50. earlier than the second.  The default block is [ :a :b | a <= b ], but I
  51. will accept any block that conforms to the above criteria.' !
  52.  
  53. !SortedCollection class methodsFor: 'instance creation'!
  54.  
  55. new
  56.     ^self sortBlock: [ :a :b | a <= b ]
  57. !
  58.  
  59. sortBlock: aSortBlock
  60.     ^super new setSortBlock: aSortBlock
  61.  
  62. !!
  63.  
  64.  
  65.  
  66. !SortedCollection methodsFor: 'basic'!
  67.  
  68. addFirst: anObject
  69.     self shouldNotImplement
  70. !
  71.  
  72. addLast: anObject
  73.     self shouldNotImplement
  74. !
  75.  
  76. at: index put: anObject
  77.     self shouldNotImplement
  78. !
  79.  
  80. add: newObject after: oldObject
  81.     self shouldNotImplement
  82. !
  83.  
  84. add: newObject before: oldObject
  85.     self shouldNotImplement
  86. !
  87.  
  88. add: anObject
  89.     "Add anObject into the collection at the proper place using bubble sort."
  90.     "### not real happy with the way this is coded"
  91.     super addFirst: anObject.
  92.     firstIndex + 1 to: lastIndex do:
  93.         [ :i | (sortBlock value: (self basicAt: i)
  94.                       value: anObject)
  95.                   ifTrue: [ self basicAt: i - 1 put: (self basicAt: i) ]
  96.           ifFalse: [ self basicAt: i - 1 put: anObject.
  97.                      ^anObject ] ].
  98.     self basicAt: lastIndex put: anObject.
  99.     ^anObject
  100. !!
  101.  
  102.  
  103.  
  104. !SortedCollection methodsFor: 'instance protocol'!
  105. sortBlock
  106.     ^sortBlock
  107. !
  108.  
  109. sortBlock: aSortBlock
  110.     "Change the sort criteria for a sorted collection, resort the elements of 
  111.     the collection, and return it."
  112.     | newSortedCollection |
  113.     newSortedCollection _ SortedCollection sortBlock: aSortBlock.
  114.     self do: [ :element | newSortedCollection add: element ].
  115.     ^self become: newSortedCollection
  116. !!
  117.  
  118.  
  119.  
  120. !SortedCollection methodsFor: 'enumerating'!
  121.  
  122. collect: aBlock
  123.     | newOrderedCollection |
  124.     newOrderedCollection _ self copyEmpty.
  125.     self do: [ :element | newOrderedCollection add: (aBlock value: element) ].
  126.     ^newOrderedCollection
  127. !!
  128.  
  129.  
  130.  
  131. !SortedCollection methodsFor: 'copying'!
  132.  
  133. copyEmpty
  134.     ^(super copyEmpty) setSortBlock: sortBlock
  135. !!
  136.  
  137.  
  138.  
  139. !SortedCollection methodsFor: 'private methods'!
  140. setSortBlock: aSortBlock
  141.     sortBlock _ aSortBlock
  142. !
  143.  
  144. growTo: anInteger
  145.     | newCollection |
  146.     newCollection _ super growTo: anInteger.
  147.     newCollection setSortBlock: sortBlock.
  148.     ^newCollection
  149. !!
  150.